<?php
	if(isset($_GET['id']) && isset($_GET['pin']))
	{
		$MaHopDong = trim($_GET['id']);
		$PIN        = trim($_GET['pin']);
		include_once '../classes/giaovien_bs.php';
		include_once '../classes/thuvienham.php';
		include_once '../classes/hopdong_bs.php';
		include_once '../classes/giaovienvaitro_bs.php';
		
		$HopDong = new hopdong_bs();
		$HopDong->SetMaHopDong($MaHopDong);
		$DHopDong = $HopDong->LayDanhSach();
		
		if($DHopDong == null)
			header('Location: index.php?act=hopdong');
		elseif(trim($DHopDong[0]['PIN']) == $PIN)
		{
?>
	<div class="FormCapNhat">
		<div class="Title">
			Thông tin chi tiết hợp đồng - <?php echo $DHopDong[0]['MaHopDong']; ?>
			<div style="float:right">
				<a href="index.php?act=hopdong_capnhat&id=<?php echo $MaHopDong; ?>&pin=<?php echo $PIN; ?>">Cập nhật</a>
			</div>
		</div>
		<?php
			$GiaoVien = new giaovien_bs();
			$GiaoVien->SetMaGiaoVien($DHopDong[0]['MaGiaoVien']);
			$DGiaoVien = $GiaoVien->LayDanhSach();
			
		?>
		<div class="Left">
			<table width="100%">
				<tr>
					<th colspan="2">Thông tin giáo viên</th>
				</tr>
				<tr>
					<td width="20%">Mã giáo viên</td>
					<td><input name="MaHocVien" type="text" readonly="true" value="<?php echo $DGiaoVien[0]['MaGiaoVien']; ?>"/></td>
				</tr>
				<tr>
					<td>Họ tên</td>
					<td><input readonly="true" name="HoTen" style="font-weight:bold" type="text" value="<?php echo $DGiaoVien[0]['HoTen']; ?>"/></td>
				</tr>
				<tr>
					<td>Ngày sinh</td>
					<td><input readonly="true" type="text" value="<?php echo ConvertDate($DGiaoVien[0]['NgaySinh']); ?>"/></td>
				</tr>
				<tr>
					<td>Giới tính</td>
					<td>
						<input disabled="true" type="radio" id="radioNam" name="GioiTinh" class="css-radio" <?php echo ($DGiaoVien[0]['GioiTinh'] == 0 ? 'checked="checked"' : '');?>/>
						<label for="radioNam" class="css-labelRadio">Nam</label>
						<input disabled="true" type="radio" id="radioNu" name="GioiTinh" class="css-radio" <?php echo ($DGiaoVien[0]['GioiTinh'] == 1 ? 'checked="checked"' : '');?>/>
						<label for="radioNu" class="css-labelRadio">Nữ</label>
					</td>
				</tr>
				
				<tr>
					<td>Điện thoại</td>
					<td><input readonly="true" name="DienThoai" type="text" value="<?php echo $DGiaoVien[0]['DienThoai']; ?>"/></td>
				</tr>
				<tr>
					<td>Email</td>
					<td><input readonly="true" name="Email" type="text" value="<?php echo $DGiaoVien[0]['Email']; ?>"/></td>
				</tr>
			</table>
			<table width="100%">
				<tr>
					<th colspan="2">Thông tin hợp đồng</th>
				</tr>
				<tr>
					<td width="20%">Mã hợp đồng</td>
					<td><input name="MaHocVien" type="text" readonly="true" value="<?php echo $DHopDong[0]['MaHopDong']; ?>"/></td>
				</tr>
				<tr>
					<?php
						$GVVT = new giaovienvaitro_bs();
						$GVVT->SetMaVaiTro($DHopDong[0]['MaVaiTro']);
						$DGVVT = $GVVT->LayDanhSach();
						$TVT = $DGVVT == null ? "" : $DGVVT[0]['TenVaiTro'];
					?>
					<td>Vai trò giáo viên</td>
					<td><input readonly="true" name="HoTen" type="text" value="<?php echo $TVT; ?>"/></td>
				</tr>
				<tr>
					<td>Lương tiết dạy</td>
					<td><input readonly="true" type="text" value="<?php echo $DHopDong[0]['LuongMoiTiet']; ?>"/></td>
				</tr>
				
				<tr>
					<td>Từ ngày</td>
					<td><input readonly="true" name="DiaChi" type="text" value="<?php echo ConvertDate($DHopDong[0]['TuNgay']); ?>"/></td>
				</tr>
				<tr>
					<td>Đến ngày</td>
					<td><input readonly="true" name="DienThoai" type="text" value="<?php echo ConvertDate($DHopDong[0]['DenNgay']); ?>"/></td>
				</tr>
				<tr>
					<td>Ngày ký</td>
					<td><input readonly="true" name="Email" type="text" value="<?php echo ConvertDate($DHopDong[0]['NgayKy']); ?>"/></td>
				</tr>
			</table>
		</div>
		<div class="Right">
			
			<table width="100%">
				<tr>
					<th colspan="2">Thông tin lớp học</th>
				</tr>
				<tr>
					<td width="35%">Lớp học</td>
					<td><input type="text" readonly="true" value="<?php echo $DHopDong[0]['MaLopHoc']; ?>" /></td>
				</tr>
				<?php
					include_once '../classes/khoahoc_bs.php';
					include_once '../classes/lophoc_bs.php';
					
					$LopHoc = new lophoc_bs();
					$LopHoc->SetMaLopHoc($DHopDong[0]['MaLopHoc']);
					$DLopHoc = $LopHoc->LayDanhSach();
					
					$KhoaHoc = new khoahoc_bs();
					$KhoaHoc->SetMaKhoaHoc($DLopHoc[0]['MaKhoaHoc']);
					$DKhoaHoc = $KhoaHoc->LayDanhSach();
				?>
				<tr>
					<td>Khóa học</td>
					<td>
						<input type="text" readonly="true" value="<?php echo $DKhoaHoc[0]['TenKhoaHoc']; ?>"/>
					</td>
				</tr>
				<tr>
					<td>Khai giảng</td>
					<td>
						<input readonly="true" name="SoMay" type="text" value="<?php echo ConvertDate($DKhoaHoc[0]['NgayKhaiGiang']); ?>" />
					</td>
				</tr>
				<tr>
					<td>Ngày bắt đầu</td>
					<td>
						<?php
							include_once '../classes/buoihoc_bs.php';
							$BuoiHoc = new buoihoc_bs();
							$BuoiHoc->SetMaLopHoc($DHopDong[0]['MaLopHoc']);
							$DBuoiHoc = $BuoiHoc->LayDanhSach();
							$NgayBatDau = $DBuoiHoc == null ? "Chưa học buổi nào" : ConvertDate($DBuoiHoc[0]['NgayHoc']);
						?>
						<input readonly="true" name="SoMay" type="text" value="<?php echo $NgayBatDau; ?>" />
					</td>
				</tr>
				<tr>
					<td>Tổng thời lượng</td>
					<?php
						include_once '../classes/chuongtrinhhoc_bs.php';
						$ChuongTrinhHoc = new chuongtrinhhoc_bs();
						$ChuongTrinhHoc->SetMaChuongTrinh($DLopHoc[0]['MaChuongTrinh']);
						$DChuongTrinhHoc = $ChuongTrinhHoc->LayDanhSach();
						$SoTiet = $DChuongTrinhHoc == null ? "" : $DChuongTrinhHoc[0]['SoTiet'] . " tiết";
					?>
					<td>
						<input readonly="true" name="SoMay" type="text" value="<?php echo $SoTiet; ?>" />
					</td>
				</tr>
				<tr>
					<td>Đã học</td>
					<?php
						$TietDaHoc = 0;
						if($DBuoiHoc != null)
						{
							for($i = 0; $i < count($DBuoiHoc); $i++)
							{
								$TietDaHoc += (float)$DBuoiHoc[$i]['SoTietHoc'];
							}
						}
					?>
					<td>
						<input readonly="true" name="SoMay" type="text" value="<?php echo "~ " . round($TietDaHoc) . " tiết, trong " . ($DBuoiHoc == null ? 0 : count($DBuoiHoc)) . " buổi"; ?>" />
					</td>
				</tr>
				<tr>
					<td>Buổi học gần nhất</td>
					<?php
						$BuoiGanNhat = $DBuoiHoc == null ? "Chưa học buổi nào" : ConvertDate($DBuoiHoc[count($DBuoiHoc) - 1]['NgayHoc']);
					?>
					<td>
						<input readonly="true" name="SoMay" type="text" value="<?php echo $BuoiGanNhat; ?>" />
					</td>
				</tr>
			</table>
		</div>
		
		<div class="Bottom">
			<a href="index.php?act=hopdong_danhsach" class="LinkBackToList">Danh mục hợp đồng</a>
		</div>
	</div>
<?php
			$HopDong->SetPIN(GeneratePIN());
			$UpHopDong = $HopDong->CapNhatPIN();
		}
		else
			header('Location: index.php?act=hopdong');
	}
	else
	{
		header('Location: index.php?act=hopdong');
	}